package com.hu.zhcc.shiro.controller.xy;

import com.alibaba.fastjson.JSONObject;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.lgx.TCheckitem;
import com.hu.zhcc.shiro.entity.xy.dto.TCheckitemDTO;
import com.hu.zhcc.shiro.entity.xy.dto.TCheckitemDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.entity.xy.vo.TCheckitemDetailVo;
import com.hu.zhcc.shiro.entity.xy.vo.TCheckitemListVO;
import com.hu.zhcc.shiro.entity.xy.vo.TCheckitemListVO;
import com.hu.zhcc.shiro.entity.xy.vo.TCheckitemListVO;
import com.hu.zhcc.shiro.service.lgx.TCheckitemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tCheckItem")
public class TCheckitemController {

    @Autowired
    private TCheckitemService service;


    @GetMapping("")
    public ResponseEntity<Page<TCheckitemListVO>> getTCheckItemList(@RequestParam(required = false) String name,
                                                                   int offset, int limit) throws ParseException {
        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            parameters.put("name", name);
        }

        Page<TCheckitemDTO> result = this.service.listCheckItem(parameters, offset, limit);
        List<TCheckitemListVO> voList = new ArrayList<TCheckitemListVO>();
        if(result.getRows()==null){
            return ResponseEntity.ok(new Page<TCheckitemListVO>(result.getTotal(), voList));
        }

        for(TCheckitemDTO dto : result.getRows()) {
            TCheckitemListVO vo = new TCheckitemListVO();
            BeanUtils.copyProperties(dto, vo);
            voList.add(vo);
        }
        return ResponseEntity.ok(new Page<TCheckitemListVO>(result.getTotal(), voList));
    }


    @PostMapping("")
    public ResponseEntity<TCheckitemListVO> saveCheckItem(@RequestBody TCheckitemListVO vo) {
        TCheckitemDTO dto = new TCheckitemDTO();
        BeanUtils.copyProperties(vo, dto);
        this.service.insertSelective(dto);
        vo.setId(dto.getId());
        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TCheckitemListVO> getById(@PathVariable("id") int id) {
        TCheckitemDTO dto = this.service.selectById(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TCheckitemListVO vo = new TCheckitemListVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }

   @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTCheckItem(@PathVariable("id") int id, @RequestBody TCheckitemDetailVo vo) {
        TCheckitemDTO dto = new TCheckitemDTO();
        BeanUtils.copyProperties(vo, dto);
        int rows = this.service.updateByPrimaryKeySelective(dto);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }


    @GetMapping("/all")
    public ResponseEntity<List<TCheckitemListVO>> listAllTCheckItem() {
        List<TCheckitemDTO> dtoList = this.service.listAllCheckItem();
        if(dtoList.size() == 0) {
            return ResponseEntity.notFound().build();
        }
        List<TCheckitemListVO> voList = new ArrayList<TCheckitemListVO>(dtoList.size());
        for(TCheckitemDTO dto : dtoList) {
            TCheckitemListVO vo = new TCheckitemListVO();
            BeanUtils.copyProperties(dto, vo);
            voList.add(vo);
        }
        return ResponseEntity.ok(voList);
    }
}
